#include <commands\server\s_accept_connection.h>
#include <app\states\connectionState.h>

REGISTER_COMMAND(SERVER_ACCEPT_CONNECTION, s_accept_connection)

s_accept_connection::s_accept_connection() : command(0, NULL) {
}

s_accept_connection::s_accept_connection(unsigned int _type_id, streamingConnection *_connection) : command(_type_id, _connection) {
}

void 
s_accept_connection::decode(stringstream &bytes_stream, int size) {
#ifdef CLIENT
  
  #ifdef DEBUG_INFO
  std::cerr << "CLIENT DECODE: s_accept_connection" << std::endl;
  #endif

  streamingConnection * conn = getconnection();
  connectionState * cstate = GET_STATE(connectionState, conn);

  if (cstate) cstate->setLoginAccepted(true);
  
  #ifdef DEBUG_INFO
    cerr << "Connection to streaming server accepted." << endl;
  #endif
  
#endif
}

void 
s_accept_connection::execute() {
#ifdef SERVER

  #ifdef DEBUG_INFO
  std::cerr << "SERVER EXECUTE: s_accept_connection" << std::endl;
  #endif

  unsigned int *id = new unsigned int[1];
  unsigned int *total_size = new unsigned int[1];

  total_size[0] = sizeof(unsigned int); // total size itself

  id[0] = getTypeId();
  total_size[0] += sizeof(unsigned int); // id size

  sendBytes(total_size, sizeof(unsigned int));
  sendBytes(id, sizeof(unsigned int));

#endif
}

